package manager;

import Database.Connectmysql;

import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeleteBook extends javax.swing.JFrame {
    private JButton deleteButton,searchButton;
    private JLabel bookId;
    private JTextField bookMes;
    private JTable jTable;
    private String[][] books = new String[1][4];

    public DeleteBook() {
        setTitle("删除图书");
        setLayout(null);
        setBounds(500, 100, 500, 300);

        deleteButton = new JButton("删除");
        searchButton = new JButton("查询");
        bookId = new JLabel("书ID");
        bookMes = new JTextField();

        deleteButton.setBounds(220,230,60,30);
        searchButton.setBounds(220,200,60,30);
        bookId.setBounds(50,20,100,30);
        bookMes.setBounds(200,20,100,30);

        //add(deleteButton);
        add(searchButton);
        add(bookId);
        add(bookMes);
        add(deleteButton);

        setVisible(true);
        searchButton.addActionListener(e -> {
            int id = Integer.parseInt(bookMes.getText());
            //System.out.println(id);
            Connectmysql cmysql = new Connectmysql(
                    "jdbc:mysql://localhost:3306/bookmanagement",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver");
            try {
                if(cmysql.open()){

                    ResultSet rst= cmysql.executeSql(
                            "select * from book where bookid=?",
                            new Object[]{id}
                    );
                    if(rst.next()){
                        books[0][0]=rst.getString(1);
                        books[0][1]=rst.getString(2);
                        books[0][2]=rst.getString(3);
                        books[0][3]=rst.getString(4);
                        jTable = new JTable(books,new String[]{"书籍id","书籍名","作者","入库时间"});
                        jTable.setBounds(25,100,450,100);
                        add(jTable);
                        setVisible(true);
                    }
                    else System.err.println("没有匹配项目");
                    rst.close();
                }
            } catch (ClassNotFoundException f) {
                f.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        });
        deleteButton.addActionListener(e -> {
            int id = Integer.parseInt(bookMes.getText());
            Connectmysql cmysql = new Connectmysql(
                    "jdbc:mysql://localhost:3306/bookmanagement",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver");
            try {
                if(cmysql.open()){

                            cmysql.executeSqlUpdate(
                            "delete from book where bookid=?",
                            new Object[]{id}
                    );
                    System.out.println("删除成功！");
                }
            } catch (ClassNotFoundException f) {
                f.printStackTrace();
            }
        });
    }


    public static void main(String[] args) {
        new DeleteBook();
    }
}
